package mi.payment.utils;

import android.view.View;

import java.io.Serializable;
import java.text.NumberFormat;

import mi.payment.R;
import mi.payment.settings.MerManagerSettings;
import mi.payment.settings.MerchantSettingsOperation;
import mi.payment.transaction.authorize.base.PreAuthorizeMainActivity;
import mi.payment.ui.base.ParentActivity;
import mi.payment.transaction.sale.HomeInputMoneyActivity;
import sunmi.sunmiui.dialog.DialogCreater;
import sunmi.sunmiui.dialog.TextHintDialog;
import sunmi.sunmiui.utils.LogUtil;

/**
 * Created by zhicheng.liu on 2017/12/26
 * address :liuzhicheng@sunmi.com
 * description : 设置常用首页工具类
 */

public class SettingHomePageUtils {
    public static final String SETTING_COMMON_HOME = "SETTING_COMMON_HOME";//设置常用首页
    public static boolean goPre = true;//首页为预授权
    private TextHintDialog mTextHintDialog;
    private SettingHome mSettingHome;

    private static volatile SettingHomePageUtils instance;

    public static SettingHomePageUtils getInstance() {
        SettingHomePageUtils settingHomePageUtils = instance;
        if (instance == null) {
            synchronized (SettingHomePageUtils.class) {
                settingHomePageUtils = instance;
                if (instance == null) {
                    settingHomePageUtils = new SettingHomePageUtils();
                    instance = settingHomePageUtils;
                }
            }
        }
        return settingHomePageUtils;
    }

    public void init(ParentActivity activity) {
        SettingHome settingHome = getSettingHome();
        if (settingHome == null) {
            settingHome = new SettingHome();
            settingHome.data = SystemDateTime.getAfterDaydate(7); // 记录一周后的时间
            settingHome.data2 = SystemDateTime.getAfterDaydate(14);
            settingHome.preCount = 0;
            settingHome.consumeCount = 0;
            settingHome.nowHome = "HomeInputMoneyActivity";
            settingHome.isWarn = true;
            settingHome.isWarnOne = true;
            LogUtil.e("lzc", "settingHome.data = " + settingHome.data + "  当前时间" + SystemDateTime.getDateString());
            setSettingHome(settingHome);
            MerchantSettingsOperation merchantSettingsOperation = MerchantSettingsOperation.getInstance();
            MerManagerSettings settings = merchantSettingsOperation.getMerManagerSettings();
            // 商户号码
            String merchantNum = settings.getMerchantNum();
            //8-12位为商户类型
            if (merchantNum == null || merchantNum.length() < 11) {
                return;
            }
            String hotel = merchantNum.substring(7, 11);
            // 酒店类商户则弹出提示
            if ("7011".equals(hotel) || "7012".equals(hotel)) {
//            if (true) {
                setHomeHotel(activity);
            }
        }
    }

    public void setHomeActivity(ParentActivity activity) {
        SettingHome settingHome = getSettingHome();
        if (settingHome != null && settingHome.isWarn) {
            LogUtil.e("lzc", "settingHome.data = " + settingHome.data + "  当前时间" + SystemDateTime.getDateString());
            LogUtil.e("lzc", "消费次数 = " + settingHome.consumeCount + "  预授权次数" + settingHome.preCount
            );
            if (SystemDateTime.getDateString().equals(settingHome.data) || SystemDateTime.getDateString().equals(settingHome.data2)) {
                if (SystemDateTime.getDateString().equals(settingHome.data) && !settingHome.isWarnOne) {
                    return;
                } else {
                    settingHome.isWarnOne = false;
                }
                if (SystemDateTime.getDateString().equals(settingHome.data2)) {
                    settingHome.isWarn = false;
                }
                int all = settingHome.consumeCount + settingHome.preCount;
                if (all > 0) {
                    NumberFormat numberFormat = NumberFormat.getInstance();
                    numberFormat.setMaximumFractionDigits(2);
                    String result = numberFormat.format((float) settingHome.consumeCount / (float) all * 100);
                    LogUtil.e("lzc", "消费占比 = " + result);
                    if (Double.parseDouble(result) >= 75 && settingHome.nowHome.equals("PreAuthorizeMainActivity")) {
                        setConsumeHome(activity);
                    } else if (Double.parseDouble(result) <= 25 && settingHome.nowHome.equals("HomeInputMoneyActivity")) {
                        setPreHome(activity);
                    }
                }
                setSettingHome(settingHome);
            }
        }
    }

    /**
     * 常用首页对象
     *
     * @return
     */
    public SettingHome getSettingHome() {
        if (mSettingHome == null) {
            Object obj = SharedPreferencesUtil.readObj(SETTING_COMMON_HOME);
            if (obj instanceof SettingHome) {
                mSettingHome = (SettingHome) obj;
            } else {
                return null;
            }
        }
        return mSettingHome;
    }

    /**
     * 设置常用首页
     *
     * @param home
     */
    private void setSettingHome(SettingHome home) {
        if (home == null) {
            return;
        }
        mSettingHome = home;
        LogUtil.e("lzc", "nowHomeActivity = " + mSettingHome.nowHome);
        SharedPreferencesUtil.saveObj(home, SETTING_COMMON_HOME);
    }

    /**
     * 设置常用首页
     *
     * @param home
     */
    public void setOftenHomePage(String home) {
        SettingHome settingHome = getSettingHome();
        if (settingHome == null) {
            return;
        }
        settingHome.nowHome = home;
        setSettingHome(settingHome);
    }

    /**
     * 增加消费笔数
     */
    public void addConsume(String transType) {
        SettingHome settingHome = getSettingHome();
        if (settingHome == null) {
            return;
        }
        if (!settingHome.isWarn) {
            return;
        }
        if (transType.equals("1") || transType.equals("22")) {
            //消费
            settingHome.consumeCount++;
        } else if (transType.equals("4") || transType.equals("10")) {
            //预授权
            settingHome.preCount++;
        }
        LogUtil.e("lzc", "刷卡次数 +1 消费类型" + transType);
        setSettingHome(settingHome);
    }

    /**
     * 增加扫码笔数
     */
    public void addScan() {
        SettingHome settingHome = getSettingHome();
        if (settingHome == null) {
            return;
        }
        if (!settingHome.isWarn) {
            return;
        }
        LogUtil.e("lzc", "扫码笔数 +1 ");
        settingHome.consumeCount++;
        setSettingHome(settingHome);
    }

    private void setHomeHotel(ParentActivity context) {
        settingHomeRemind(context, context.getString(R.string.setting_home_first_tip), context.getString(R.string.setting_common_pre), true);
    }

    private void setConsumeHome(ParentActivity context) {
        settingHomeRemind(context, context.getString(R.string.setting_home_consume_tip), context.getString(R.string.setting_common_consume), false);
    }

    private void setPreHome(ParentActivity context) {
        settingHomeRemind(context, context.getString(R.string.setting_home_pre_tip), context.getString(R.string.setting_common_pre), true);
    }

    /**
     * 弹窗提醒
     *
     * @param context
     * @param content
     * @param confirm
     * @param isConsume
     */
    private void settingHomeRemind(final ParentActivity context, String content, String confirm, final boolean isConsume) {
        LogUtil.e("lzc", "首页设置提醒  " + (isConsume ? "PreAuthorizeMainActivity" : "HomeInputMoneyActivity"));
        mTextHintDialog = DialogCreater.createTextHintDialog(context, context.getResources().getString(R.string.setting_common_home), context.getResources().getString(R.string.setting_common_cacel), confirm, content, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mTextHintDialog.dismiss();
            }
        }, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mTextHintDialog.dismiss();
                if (isConsume) {
                    setOftenHomePage("PreAuthorizeMainActivity");
                    context.openActivity(PreAuthorizeMainActivity.class);
                } else {
                    setOftenHomePage("HomeInputMoneyActivity");
                    context.openActivity(HomeInputMoneyActivity.class);
                }
                context.finish();
            }
        }, false);
        mTextHintDialog.show();
    }

    public static class SettingHome implements Serializable {
        public boolean isWarnOne = false;//是否第一次提醒
        public boolean isWarn = false;//是否提醒
        public String nowHome;//当前首页
        public String data;//日期
        public String data2;//日期
        public int consumeCount;//一般交易笔数
        public int preCount;//预授权交易笔数
    }

}
